import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;


public class Task {
private static final int INDEX_TITLE = 1;
private static final int INDEX_TIME = 2;
private static final int INDEX_TAG = 3;
private static final int INDEX_LEVEL = 4;
private static final int LEVEL_DEFAULT = 8;
private String title;
private String time;
private String tag;
private int urgencyLevel;
private boolean isFinish;
public Task(String title,String deadline) {
	this.title= title;
	this.time = deadline;
	this.tag = " ";
	this.urgencyLevel = LEVEL_DEFAULT;
	this.isFinish = false;
}
public Task() {
	
}
public Task(ArrayList<String> contents) {
	if(contents == null) {
		this.title = "remove";
		this.time = "soon";
		this.tag = "remove soon";
		this.urgencyLevel = 8;
		
	} else {
		this.title = contents.get(INDEX_TITLE);
		this.time = contents.get(INDEX_TIME);
		try {
		this.tag = contents.get(INDEX_TAG);
		} catch (Exception e) {
			this.tag ="null tag";
		}
		try {
			this.urgencyLevel = Integer.parseInt(contents.get(INDEX_LEVEL));
		} catch (Exception e) {
			this.urgencyLevel = 8;
		}
	}
	this.isFinish = false;
	
}
public String getTitle() {
	return this.title;
}
public String getTime() {
	return this.time;
}
public String getTag() {
	return this.tag;
}
public int getUrgencyLevel() {
	return this.urgencyLevel;
}
public boolean isThisTaskFinished() {
	return this.isFinish;
}
public void modifyName(String title) {
	this.title = title;
}
public void modifyTime (String time) {
	try {
		Date theDate = new SimpleDateFormat("MMMM dd yyyy", Locale.ENGLISH).parse(time);
		this.time = theDate.toString();
	} catch(Exception e) {
		e.printStackTrace();
	}
	
	
}
public void modifyTime (Date time) {
	this.time = time.toString();
}
public void modifyTag(String tag) {
	this.tag = tag;
}
public void modifyUrgencyLevel(int level) {
	this.urgencyLevel = level;
}
public void modifyUrgencyLevel(String level) {
	this.urgencyLevel = Integer.parseInt(level.trim());
}
public void finishATask() {
	this.isFinish =true;
}
public boolean contains(String keywords) {
	boolean doesContains= this.title.contains(keywords)||this.time.contains(keywords)
													   ||this.tag.contains(keywords);
	String leveltoString = Integer.toString(this.urgencyLevel);
	return (doesContains||(leveltoString.equals(keywords)));
	
	
		
	}
public boolean isValid() {
	if(this.title == null) {
		return false;
	}
	if(this.time == null) {
		return false;
	}
	else {
		try{ 
			Date theDate = new SimpleDateFormat("MMMM dd yyyy", Locale.ENGLISH).parse(this.time);
		} catch (Exception e) {
			return false;
		}
	}
	return true;
}
public int compareTo(Task compareTask) {
		return 0;
	}

	public static Comparator<Task> TaskTitleComparator = new Comparator<Task>() {
		public int compare(Task task1, Task task2) {
			String taskTitle1 = task1.getTitle().toUpperCase();
			String taskTitle2 = task2.getTitle().toUpperCase();

			return taskTitle1.compareTo(taskTitle2);
		}
	};

    public static Comparator<Task> TaskUrgencyLevelComparator = new Comparator<Task>() {
        public int compare(Task task1, Task task2) {
            int taskUrgencyLevel1 = task1.getUrgencyLevel();
            int taskUrgencyLevel2 = task2.getUrgencyLevel();

            return taskUrgencyLevel1 - taskUrgencyLevel2;
        }
    };
    
    public static Comparator<Task> TaskDateComparator = new Comparator<Task>() {
        public int compare(Task task1, Task task2) {
        	DateFormat dateFormat = new SimpleDateFormat("MMMM dd yyyy", Locale.ENGLISH);
            Date dateTask1 = null;
            Date dateTask2 = null;
            try {        
                dateTask1 = (Date) dateFormat.parse(task1.getTime());
                dateTask2 = (Date) dateFormat.parse(task2.getTime());
            } catch(Exception e) {
                e.printStackTrace();
            }
            return dateTask1.compareTo(dateTask2);
        }
    };
    public String toString() {
    	return this.title+", "+this.time+", "+this.tag+" ,"+Integer.toString(this.urgencyLevel)+". ";
    }
}


